<!-- 报价审核 -->
<template>
    <view class="container">
        <search-header ref="searchHeader" isExamine @doFilter="doFilter" @searchFn="searchFn"></search-header>
        <tab-bar-two :tabs="items" nobg="nobg" @clickTab="clickTab" :tabIndex="tabIndex"></tab-bar-two>
        <view class="main">
            <swiper
                v-if="loadOk"
                class="swiper product-main-list"
                :style="{ height: listHeight }"
                :current="tabIndex"
                @change="swiperChange">
                <swiper-item v-for="(swiperItem, swiperIndex) in items" :key="swiperIndex">
                    <examine-list
                        ref="examineList"
                        :height="listHeight"
                        :state="swiperItem.state"
                        :requestData="requestData"
                        @recall="recallFn"
                        @quotation="quotationFn"></examine-list>
                </swiper-item>
            </swiper>
        </view>
        <uni-popup ref="quotationPop" type="center" :animation="false" :safe-area="false">
            <view class="pop-return-box">
                <view class="pop-return-title">
                    <text>报价</text>
                </view>
                <view class="input-box">
                    <input
                        auto-focus
                        placeholder="请填写新报价"
                        confirm-type="search"
                        type="digit"
                        v-model="price"
                        class="btn-input" />
                    <text class="price-unit">元</text>
                </view>

                <view class="pop-action">
                    <view @click="cancelFn">取消</view>
                    <view @click="confirmFn">确定</view>
                </view>
            </view>
        </uni-popup>
        <uni-popup ref="recallPop" type="center" :animation="false" :safe-area="false">
            <view class="pop-return-box">
                <view class="pop-return-title">
                    <text>提示</text>
                </view>
                <view class="tip-box">
                    <text> 撤回后，平台将无法看到报价，是否继续？ </text>
                </view>

                <view class="pop-action">
                    <view @click="cancelRecallFn">取消</view>
                    <view @click="confirmRecallFn">确定</view>
                </view>
            </view>
        </uni-popup>
    </view>
</template>
<script>
import storage from "@/uni-sub-base/utils/storage.js"
import tabBarTwo from "@/pages_supplier/components/common/tab-bar-two.vue"
import searchHeader from "@/pages_supplier/components/quotation/search-header.vue"
import examineList from "@/pages_supplier/components/quotation/examine-list.vue"
import { getElementHeight } from "@/uni-sub-base/utils/elementUtil.js"
import { WithdrawOfferSkuPrices, EditOfferSkuPrices } from "@/pages_supplier/service/api/supplier.js"
export default {
    components: {
        tabBarTwo,
        searchHeader,
        examineList,
    },
    data() {
        return {
            listHeight: "500rpx",
            loadOk: false,
            datas: [],
            tabIndex: 0,
            items: [
                {
                    state: -1,
                    title: "全部",
                },
                {
                    state: 0,
                    title: "待审核",
                },
                {
                    state: 1,
                    title: "已同意",
                },
                {
                    state: 2,
                    title: "已驳回",
                },
                {
                    state: 3,
                    title: "草稿",
                },
            ],
            requestData: {
                searchKey: "",
                startTime: "",
                endTime: "",
                id: 0,
            },
            price: "",
            currentItem: {},
            from: 0,
        }
    },
    onLoad(e) {
        if (e.from) {
            this.from = e.from
        }
        if (e.id) {
            this.requestData.id = e.id
        }
    },
    mounted() {
        this.getListHeight()
    },
    methods: {
        getListHeight() {
            //获取商品高度
            getElementHeight(this, ".main", (e) => {
                this.listHeight = e + "px"
                this.loadOk = true
                setTimeout((e) => {
                    //#ifndef MP
                    this.refresh()
                    //#endif
                }, 300)
            })
        },

        doFilter(e) {
            this.requestData.id = 0
            this.requestData.startTime = e.startDate ? e.startDate + " 00:00:00" : ""
            this.requestData.endTime = e.endDate ? e.endDate + " 23:59:59" : ""
            this.refresh()
        },
        swiperChange(e) {
            let tabIndex = e.detail.current
            this.clickTab(tabIndex)
        },
        //tab切换
        clickTab(index) {
            if (this.tabIndex != index) {
                this.tabIndex = index
            }
        },
        //搜索
        searchFn(searchKey) {
            this.requestData.startTime = ""
            this.requestData.endTime = ""
            this.requestData.id = 0
            this.requestData.searchKey = searchKey
            this.$refs.searchHeader.cancelFilter()

            this.refresh()
        },
        refresh() {
            this.$refs.examineList.forEach((e) => {
                e.refresh(this.requestData)
            })
        },
        quotationFn(e) {
            this.currentItem = e
            this.price = this.currentItem.Price
            this.$refs.quotationPop.open()
        },
        cancelFn() {
            this.$refs.quotationPop.close()
        },
        confirmFn() {
            this.cancelFn()
            if (!this.currentItem) {
                return ""
            }
            if (this.price.trim() == "") {
                this.easyToast("请输入报价")
                return
            }
            let price = Number(this.price)
            if (price <= 0) {
                this.easyToast("请输入正确的报价")
                return
            }
            let param = {
                ID: this.currentItem.ID,
                productId: this.currentItem.ProductId,
                skuId: this.currentItem.SkuId,
                unitId: this.currentItem.UnitId,
                price: price,
                supplierId: this.currentItem.SupplierId,
                ratio: this.currentItem.Ratio,
                unitName: this.currentItem.UnitName,
                productCode: this.currentItem.ProductCode,
                skuCode: this.currentItem.SkuCode,
                attribute: this.currentItem.Attribute ? this.currentItem.Attribute : "",
                productName: this.currentItem.ProductName,
                productImage: this.currentItem.ProductImage ? this.currentItem.ProductImage : "",
                BusinessId: this.currentItem.BusinessId,
            }
            EditOfferSkuPrices(param).then((res) => {
                if (res.State == 1) {
                    this.easyToast("提交成功")
                    this.refresh()
                } else {
                    this.easyToast(res.Msg)
                }
            })
        },
        cancelRecallFn() {
            this.$refs.recallPop.close()
        },
        confirmRecallFn() {
            this.cancelRecallFn()
            if (!this.currentItem) {
                return
            }
            let param = {
                id: this.currentItem.ID,
            }
            let path = "?id=" + this.currentItem.ID
            WithdrawOfferSkuPrices(param, path).then((res) => {
                console.log(res)
                if (res.State == 1) {
                    this.easyToast("提交成功")
                    this.refresh()
                } else {
                    this.easyToast(res.Msg)
                }
            })
        },
        recallFn(e) {
            this.currentItem = e
            this.$refs.recallPop.open()
        },
    },
}
</script>

<style lang="scss"></style>
